import Vue from 'vue'
import Router from 'vue-router'
import {
  WALLET_RECHARGE,
  WALLET_RETURN,
  BUS_CONSUME,
  BUS_CONSUME_RETURN,
  MALL_CONSUME,
  MALL_CONSUME_RETURN,
  ICCARD_CONSUME,
  ICCARD_CONSUME_RETURN
} from '../constants/TradeType'
import { getUrlParamByKey } from 'vue-xiaobu-utils'

Vue.use(Router)
// 以下动态导入代码分割，routebill是指定页面的文件夹，MallConsumeBill是分割文件名称
const MallConsumeBill = () =>
  import(
    /* webpackChunkName: "routebill/MallConsumeBill" */ '../page/MallConsumeBill.vue'
  )
const MallConsumeReturnBill = () =>
  import(
    /* webpackChunkName: "routebill/MallConsumeReturnBill" */ '../page/MallConsumeReturnBill.vue'
  )
const BusConsumeBill = () =>
  import(
    /* webpackChunkName: "routebill/BusConsumeBill" */ '../page/BusConsumeBill.vue'
  )
const BusConsumeReturnBill = () =>
  import(
    /* webpackChunkName: "routebill/BusConsumeReturnBill" */ '../page/BusConsumeReturnBill.vue'
  )
const WalletRechargeBill = () =>
  import(
    /* webpackChunkName: "routebill/WalletRechargeBill" */ '../page/WalletRechargeBill.vue'
  )
const WalletReturnBill = () =>
  import(
    /* webpackChunkName: "routebill/WalletReturnBill" */ '../page/WalletReturnBill.vue'
  )
const ICCardConsumeBill = () =>
  import(
    /* webpackChunkName: "routebill/ICCardConsumeBill" */ '../page/ICCardConsumeBill.vue'
  )
const ICCardConsumeReturnBill = () =>
  import(
    /* webpackChunkName: "routebill/ICCardConsumeReturnBill" */ '../page/ICCardConsumeReturnBill.vue'
  )

const router = new Router({
  routes: [
    {
      path: '/mallConsumeBill',
      component: MallConsumeBill,
      name: 'mallConsumeBill'
    },
    {
      path: '/mallConsumeReturnBill',
      component: MallConsumeReturnBill,
      name: 'mallConsumeReturnBill'
    },
    {
      path: '/busConsumeBill',
      component: BusConsumeBill,
      name: 'busConsumeBill'
    },
    {
      path: '/busConsumeReturnBill',
      component: BusConsumeReturnBill,
      name: 'busConsumeReturnBill'
    },
    {
      path: '/walletRechargeBill',
      component: WalletRechargeBill,
      name: 'walletRechargeBill'
    },
    {
      path: '/walletReturnBill',
      component: WalletReturnBill,
      name: 'walletReturnBill'
    },
    {
      path: '/icCardConsumeBill',
      component: ICCardConsumeBill,
      name: 'icCardConsumeBill'
    },
    {
      path: '/icCardReturnBill',
      component: ICCardConsumeReturnBill,
      name: 'icCardReturnBill'
    }
  ]
})

// 全局路由守护
router.beforeEach((to, from, next) => {
  // 根据url上带入的交易类型,指向不同的路由

  let path
  switch (parseInt(getUrlParamByKey('tradetype'))) {
    case WALLET_RECHARGE:
      path = '/walletRechargeBill'
      break
    case WALLET_RETURN:
      path = '/walletReturnBill'
      break
    case BUS_CONSUME:
      path = '/busConsumeBill'
      break
    case BUS_CONSUME_RETURN:
      path = '/busConsumeReturnBill'
      break
    case MALL_CONSUME:
      path = '/mallConsumeBill'
      break
    case MALL_CONSUME_RETURN:
      path = '/mallConsumeReturnBill'
      break
    case ICCARD_CONSUME:
      path = '/icCardConsumeBill'
      break
    case ICCARD_CONSUME_RETURN:
      path = '/icCardReturnBill'
      break
    default:
      // 默认的页面路由，用于调试
      path = '/busConsumeBill'
      break
  }
  if (path !== to.path) {
    // next({ path: path })
    router.replace({ path: path })
  }
  next()
})

export default router
